//
//  BPAddAddressViewController.m
//  BabyPhotograph
//
//  Created by cesh on 2017/2/17.
//  Copyright © 2017年 cesh. All rights reserved.
//

#import "BPAddAddressViewController.h"
#import "BPAddressSelectViewController.h"
#import "UITextView+Placeholder.h"

@interface BPAddAddressViewController ()<BPAddressSelectProtocol>


@property (weak, nonatomic) IBOutlet UIView *addressLabel;

@property (weak, nonatomic) IBOutlet UIButton *defaultAddressBtn;

@property (weak, nonatomic) IBOutlet UILabel *location;
@property (weak, nonatomic) IBOutlet UITextView *location_ext;

@property (assign, nonatomic) CGFloat latitude;
@property (assign, nonatomic) CGFloat longtitude;

@end

@implementation BPAddAddressViewController

- (void)viewDidLoad {
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(saveLocation)];
    _defaultAddressBtn.selected = YES;
    
    [_addressLabel addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pushAddressChooseVC)]];
}

- (void)pushAddressChooseVC {
    BPAddressSelectViewController *addressSelectVC = [[BPAddressSelectViewController alloc]init];
    addressSelectVC.delegate = self;
    [self.navigationController pushViewController:addressSelectVC animated:YES];
}
//逆传代理方法
- (void)addressSelectViewController:(BPAddressSelectViewController *)addressSelectViewController selectedAddress:(NSString *)selectedAddress latitude:(CGFloat)latitude longtitude:(CGFloat)longtitude{
    for (UIView *address in self.addressLabel.subviews) {
        if ([address isKindOfClass:[UILabel class]]) {
            UILabel *addLbl = (UILabel *)address;
            addLbl.text = selectedAddress;
        }
    }
    self.latitude = latitude;
    self.longtitude = longtitude;
}

- (void)saveLocation {
    NSDictionary *user = [[NSUserDefaults standardUserDefaults]objectForKey:@"user"];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //接收类型不一致请替换一致text/html或别的
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
                                                         @"text/html",
                                                         @"image/jpeg",
                                                         @"image/png",
                                                         @"application/octet-stream",
                                                         @"text/json",
                                                         @"text/plain",
                                                         nil];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"token"] = user[@"token"];
    dict[@"lat"] = [NSString stringWithFormat:@"%f", self.latitude];
    dict[@"lng"] = [NSString stringWithFormat:@"%f", self.longtitude];
    dict[@"local"] = self.location.text;
    dict[@"is_default"] = self.defaultAddressBtn.isSelected ? @"1" : @"0";
    if (self.old_id) {
        dict[@"old_id"] = self.old_id;
    }
    //根据用户是否填写详细地址添加字段
    if ([self.location_ext.text length] > 0) {
        dict[@"local_ext"] = self.location_ext.text;
    }
    [SVProgressHUD showWithStatus:@"保存中..."];
    [manager POST:[NSString stringWithFormat:@"%@?c=User&&a=saveAddress",requestURL]
       parameters:dict
     progress:^(NSProgress * _Nonnull uploadProgress) {
         
     } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
         if ([responseObject[@"state"] isEqualToString:@"SUCCESS"]) {
             NSLog(@"%@", responseObject[@"data"]);
             [SVProgressHUD dismiss];
             //pop至上一级并且刷新地址tableview
             if ([self.delegate respondsToSelector:@selector(reloadAddress)]) {
                 [self.delegate reloadAddress];
             }
             [self.navigationController popViewControllerAnimated:YES];
         }
         else {
             NSLog(@"%@", responseObject[@"data"]);
         }
     } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
         NSLog(@"%@", error);
     }];
}

- (IBAction)clickDefaultBtn:(UIButton *)sender {
    sender.selected = !sender.isSelected;
}

@end
